package giftmail.mq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

@Service("producerService")
public class ProducerServiceImpl implements IProducerService {

	@Autowired
	private JmsTemplate jmsTemplate;

    @Autowired  
    @Qualifier("responseQueue")  
    private Destination responseDestination;
    
	/**
	 * 通过jmsTemplate,向Destination发送纯文本消息的方法sendMessage
	 */
	public void sendMessage(Destination destination, final String message) {
		System.out.println("---------------生产者发送消息-----------------");
		System.out.println("---------------生产者发了一个消息：" + message);
		jmsTemplate.send(destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
//				return session.createTextMessage(message);
				TextMessage textMessage = session.createTextMessage(message);
				//修改代码
				//在生产者发送消息之前先指定该消息对应的回复目的地为一个叫responseQueue的队列目的地
                textMessage.setJMSReplyTo(responseDestination);  
                return textMessage;
			}
		});
	}

}
